import { defineStore } from "pinia";
import {queryTree} from '../api/dept'
import Validator from '@/utils/Validator';
const useDeptStore = defineStore('dept',()=>{
    // 部门选择树
    const deptTree = ref([])
    // 获取部门选择树
    function getTree(){
        return new Promise(function(resolve,reject){
            if(Validator.isEmpty(deptTree.value)){
                queryTree().then(res=>{
                    console.log('run query dept')
                    deptTree.value = res
                    //console.log(res)
                    resolve(deptTree.value)
                })
            }else{
                resolve(deptTree.value)
            }
        })
    }
    // 更新部门选择树
    function freshTree(){
        return new Promise(function(resolve,reject){
            queryTree().then(res=>{
                deptTree.value = res
                resolve(deptTree.value)
            })
        })
    }
    return {deptTree, getTree, freshTree}
})
export default useDeptStore